//
//  NewsCategoryManagerViewController.m
//  pushNews
//
//  Created by Jobs on 3/20/17.
//  Copyright © 2017 Iori. All rights reserved.
//

#import "NewsCategoryManagerViewController.h"
#import "NewsCategoryCollectionViewCell.h"
#import "CategoryHeaderView.h"
#import "IoriHeadFile.h"
#import "GetChannelListApi.h"
#import "ChannelListEntity.h"
#import "Header.h"

@interface NewsCategoryManagerViewController ()
{
    BOOL isManaager;
    UIStatusBarStyle barStyle;
    NSMutableDictionary *dictDataSource;
    NSMutableArray<NSMutableDictionary*> *dataArraySelected;
    NSMutableArray<NSMutableDictionary*> *dataArrayUnSelected;
    NSString *filePath;
}
@property (strong, nonatomic) NSArray<ChannelEntity*> *dataSource;
@property (strong, nonatomic) NSMutableArray<ChannelEntity*> *selectedChannel;
@property (nonatomic, copy) NSArray<NSString*> *selectedChannelID;
@end

@implementation NewsCategoryManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.collectionView.alwaysBounceVertical = YES;
    self.selectedChannel = [NSMutableArray arrayWithCapacity:10];
    if([self.newsCategoryUpdateDelegate respondsToSelector:@selector(getAleadySelectedChannelID)])
    {
        NSString *strChannelID = [self.newsCategoryUpdateDelegate getAleadySelectedChannelID];
        self.selectedChannelID = [strChannelID componentsSeparatedByString:@","];
        
    }
    [self.collectionView addPullToRefreshWithActionHandler:^{
        GetChannelListApi *api = [GetChannelListApi new];
        api.member_id = [UserInfo sharedUser].admin.admin_id;
        WS(weakSelf);
        [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
            [weakSelf.collectionView.pullToRefreshView stopAnimating];
            if([IoriJsonHelper getErrorCode:request] == 100)
            {
                ChannelListEntity *channelListEntity = [ChannelListEntity yy_modelWithJSON:[IoriJsonHelper getDictForKey:@"result" fromDict:request.responseJSONObject]];
                NSMutableArray<ChannelEntity*> *tempArray = [NSMutableArray arrayWithArray:channelListEntity.channel_list];
                [tempArray addObjectsFromArray:channelListEntity.my_channel];
                weakSelf.dataSource = tempArray;
                [tempArray enumerateObjectsUsingBlock:^(ChannelEntity*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if([weakSelf.selectedChannelID containsObject:obj.channel_id])
                    {
                        [weakSelf.selectedChannel addObject:obj];
                    }
                }];
                [weakSelf.collectionView reloadData];
            }
            else
            {
                [weakSelf showMessage:[IoriJsonHelper getError:request] ];
            }
        } failure:^(YTKBaseRequest *request) {
            [weakSelf showMessage:[request responseString]];
            [weakSelf.collectionView.pullToRefreshView stopAnimating];
        }];
    }];
    [self.collectionView triggerPullToRefresh];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

-(void)exit
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)btnChooseOk_click:(UIButton *)sender
{
    WS(weakSelf);
    if([weakSelf.newsCategoryUpdateDelegate respondsToSelector:@selector(newsCategorySelected:)])
    {
        [weakSelf.newsCategoryUpdateDelegate newsCategorySelected:self.selectedChannel];
    }
    [weakSelf exit];
}

#pragma mark collectionViewDelegate


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ChannelEntity *entity = self.dataSource[indexPath.item];
    NSString *collectionViewTitle = entity.channel_name;
    NewsCategoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell.labTitle setText:collectionViewTitle];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:entity.channel_pic]];
    
    if([self.selectedChannel containsObject:entity] == NO)
    {
        cell.btnDelete.hidden = YES;
    }
    else
    {
        cell.btnDelete.hidden = NO;
    }
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NewsCategoryCollectionViewCell *cell = (NewsCategoryCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    ChannelEntity *entity = [self.dataSource objectAtIndex:indexPath.item];
    if([self.selectedChannel containsObject:entity] == NO)
    {
        [self.selectedChannel addObject:entity];
        cell.btnDelete.hidden = NO;
    }
    else
    {
        [self.selectedChannel removeObject:entity];
        cell.btnDelete.hidden = YES;
    }
    [collectionView reloadItemsAtIndexPaths:@[indexPath]];
}

@end
